var Child = {
	template: '<div class="noMsg"><img class="noMsg-svg" src="../../images/noMsg.png"><p class="noMsg-p">暂无信息</p></div>'
};
new Vue({
	el: '#app',
	data: {
		uid: '',
		tid: '4',
		cid: '',
		isShow: false,
		classList: [],
		t: [],
		page: '1',
		url: '',
		categoryList: [],
		cateH: '',
		isNoM: false
	},
	mounted: function() {
		console.log("mounted end");
		this.uid = $api.getStorage('uid');
		this.init();
	},
	components: {
		// <my-component> 将只在父模板可用
		'my-component': Child
	},
	methods: {
		init: function() {
			var self = this;
			self.getClass();
			apiready = function() {
				var fid = api.pageParam;
				console.log(fid.tid);
				self.tid = fid.tid;
				//loading加载
				self.isS();
				//滚动到底部加载更多
				api.addEventListener({
					name: 'scrolltobottom',
					extra: {
						threshold: 0 //设置距离底部多少距离时触发，默认值为0，数字类型
					}
				}, function(ret, err) {
					//alert('已滚动到底部');
					self.page++;
					if(self.page > self.pageCount) {
						api.toast({
							msg: '没有更多了',
							duration: 1000,
							location: 'bottom'
						});
					} else {
						self.getClass();
					}
				});
			}
		},
		isS: function() {
			var self = this;
			var UILoading = api.require('UILoading');
			if(self.isShow) {
				console.log(self.loadingId);
				UILoading.closeFlower({
					id: self.loadingId
				});
			} else {
				UILoading.flower({
					center: {
						x: api.winWidth / 2.0,
						y: api.winHeight / 2.0
					},
					size: 30,
					fixed: true
				}, function(ret) {
					console.log(JSON.stringify(ret));
					self.loadingId = ret.id;
				});

				self.getClass();
			}
		},
		//是否有数据
		isNoMsg: function(d) {
			console.log(d);
			if(d.length == 0) {
				this.isNoM = true;
			} else {
				this.isNoM = false;
			}
		},
		//获得课程的列表
		getClass: function() {
			var self = this;
			console.log(self.cid);
			var data = {
				page: self.page,
				teacher_id: self.tid
			};
			$.post(base + config.study_recording_list, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.t = msg.data;
					for(var i = 0; i < self.t.length; i++) {
						self.classList.push(self.t[i]);
					};
					self.pageCount = msg.page_count;
					self.url = msg.url;
					self.$nextTick(function() {
						// DOM 现在更新了
						// `this` 绑定到当前实例
						$("#main").css("margin-top", (75) + "px");
					});
					self.isNoMsg(self.classList);
					self.isShow = true;
					self.isS();
				} else {
					self.isShow = true;
					self.isS();
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//关闭本页面
		close: function() {
			api.closeWin();
		},
		//打开录播课判断
		open: function(id) {
			var self = this;
			var data = {
				id: id,
				user_id: self.uid
			};
			$.post(base + config.study_recording_ver, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					if(msg.can_download == 0) {
						api.confirm({
							title: '是否兑换',
							msg: msg.msg,
							buttons: ['确定', '取消']
						}, function(ret, err) {
							var index = ret.buttonIndex;
							console.log(index);
							if(index == 1) {
								self.exchange(msg.id);
							}
						});
					} else {
						self.file_url = msg.url + msg.file_path + msg.file_name;
						console.log(self.file_url);
						self.download(self.file_url);
					}
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//兑换
		exchange: function(id) {
			var self = this;
			var data = {
				id: id,
				user_id: self.uid
			};
			$.post(base + config.study_recording_exchange, data, function(msg) {
				console.log(msg);
				if(msg.error == 0) {
					self.file_url = msg.url + msg.file_path + msg.file_name;
					console.log(self.file_url);
					self.download(self.file_url);
				} else {
					console.log(msg.error_msg);
					api.toast({
						msg: msg.error_msg,
						duration: 2000,
						location: 'middle'
					});
				}
			}, 'json');
		},
		//调用原生
		download: function(url) {
			//			alert(url);
			api.sendEvent({
				name: 'play_video',
				extra: {
					url: url
				}
			});
		}
	}
})